﻿using Basic.FlowModel.DB;
using Basic.FlowRemoteModel.AuditSignFile.Model;
using WeDonekRpc.Helper;
using WeDonekRpc.Helper.IdGenerator;
using WeDonekRpc.SqlSugar;

namespace Basic.FlowDAL.lmpl
{
    internal class FlowAuditSignFileDAL : BasicDAL<DBFlowAuditSignFile, long>, IFlowAuditSignFileDAL
    {
        public FlowAuditSignFileDAL ( IRepository<DBFlowAuditSignFile> basicDAL ) : base(basicDAL, "audit.sign.file")
        {
        }
        public void SetImgUri ( long id, string imgUri )
        {
            if ( !this._BasicDAL.Update(a => a.ImgUri == imgUri, a => a.Id == id) )
            {
                throw new ErrorException("flow.audit.sign.file.set.fail");
            }
        }
        public long Add ( AuditSignFileAdd add )
        {
            DateTime now = DateTime.Now;
            DBFlowAuditSignFile db = new DBFlowAuditSignFile
            {
                Id = IdentityHelper.CreateId(),
                AddTime = now,
                FileId = add.FileId,
                FormFileId = add.FormFileId,
                SourceFileId = add.FileId,
                FileName = add.FileName,
                FileUri = add.FileUri,
                LastEditEmpId = add.EmpId,
                LastEditTime = now,
                LogId = add.LogId,
            };
            DBFlowAuditSignLog log = new DBFlowAuditSignLog
            {
                Id = IdentityHelper.CreateId(),
                SignEmpId = add.EmpId,
                CertFileId = 0,
                FileId = add.FileId,
                FileUri = add.FileUri,
                IsVoid = false,
                LogId = add.LogId,
                SignFileId = db.Id,
                SignItemId = add.ItemId,
                SignTime = now,
                SignType = FlowRemoteModel.FlowSignType.原文件
            };
            ISqlQueue<DBFlowAuditSignFile> queue = this._BasicDAL.BeginQueue();
            queue.Insert(db);
            queue.InsertBy(log);
            _ = queue.Submit();
            return db.Id;
        }

    }
}
